﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DTO;
using System.Data.SqlClient;
using System.Data;


namespace DAO
{
    public class HoaDonBanVeDAO
    {
        public List<HoaDonBanVeDTO> LayDanhSachHoaDonBanVe()
        {
            List<HoaDonBanVeDTO> listHDBV = new List<HoaDonBanVeDTO>();
            SqlConnection conn = null;
            SqlDataReader reader = null;
            try
            {
                String sql = "Select MaHD, hd.NgayLap, TongTien, GiamGia, hd.MaKH, Hoten from HoaDonBanVe hd, KhachHang kh where hd.MaKH = kh.MaKH";

                conn = DataProvider.ConnectDB();
                SqlCommand cm = new SqlCommand(sql, conn);
                reader = cm.ExecuteReader();
                while (reader.Read())
                {
                    HoaDonBanVeDTO hdbvDTO = new HoaDonBanVeDTO();
                    hdbvDTO.MaHD = reader.GetInt32(0);
                    hdbvDTO.NgayLap = reader.GetDateTime(1);
                    hdbvDTO.TongTien = Convert.ToDouble( reader.GetDecimal(2));
                    hdbvDTO.GiamGia = Convert.ToDouble(reader.GetDecimal(3));
                    
                    hdbvDTO.MaKH = reader.GetInt32(4);
                    hdbvDTO.TenKH = reader.GetString(5);
                    listHDBV.Add(hdbvDTO);
                }
            }
            catch (Exception ex)
            { String e = ex.ToString(); }
            finally
            { conn.Close(); }
            return listHDBV;
        }

        public int ThemHoaDonBanVe(HoaDonBanVeDTO hdbvDTO)
        {
            int ID = -1;
            SqlConnection conn = null;
            try
            {
                conn = DataProvider.ConnectDB();
                String sql = "INSERT INTO HoaDonBanVe(NgayLap,TongTien,GiamGia,MaKH)";
                sql += "VALUES(@NgayLap,@TongTien,@GiamGia,@MaKH)";
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = sql;
                SqlParameter para;
                para = cmd.Parameters.Add("@NgayLap", SqlDbType.Date);
                para.Value = hdbvDTO.NgayLap;
                para = cmd.Parameters.Add("@TongTien", SqlDbType.Decimal);
                para.Value = hdbvDTO.TongTien;
                para = cmd.Parameters.Add("@GiamGia", SqlDbType.Decimal);
                para.Value = hdbvDTO.TongTien;
                para = cmd.Parameters.Add("@MaKH", SqlDbType.Int);
                para.Value = hdbvDTO.MaKH;

                cmd.ExecuteNonQuery();

                // lay id vua them

                sql = "Select @@IDENTITY";
                cmd = new SqlCommand(sql, conn);
                ID = Convert.ToInt32(cmd.ExecuteScalar());


            }
            catch (Exception ex)
            {
               
            }
            finally
            {
                conn.Close();
            }
            return ID;
        }
    }
}
